﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTTT.DataAccess;
using HTTT.Utilities;
using log4net;
using HTTT.DTO;

namespace HTTT.Bussiness
{
    public class HT_OUBO
    {
        public IEnumerable<HT_OU> GetAllHT_OU()
        {
            HT_OUDAO _dao = new HT_OUDAO();
            IEnumerable<HT_OU> data = _dao.GetAllHT_OU();
            foreach (HT_OU ou in data)
            {
                string[] split = ou.DistinguishedName.Split(',');
                StringBuilder sbBack = new StringBuilder();
                for (int i = split.Count() - 1; i >= 0; i--)
                {
                    sbBack.Append(split[i]);
                    sbBack.Append(",");
                    if (i == split.Count() - 1)
                    {
                        ou.Name = "├ " + ou.Name;
                    }
                    if (i > 2)
                    {
                        ou.Name = "░     " + ou.Name;
                    }

                }         
                ou.distinguishedNameSort = sbBack.ToString();
            }
            var q = from m in data
                    orderby m.distinguishedNameSort
                    select m;
            return q;
        }
        public HT_OU GetHT_OUById(Guid id)
        {
            HT_OUDAO _dao = new HT_OUDAO();
            return _dao.GetHT_OUByID(id);
        }
        public bool UpdateHT_OU(HT_OU inHT_OU)
        {
            HT_OUDAO _dao = new HT_OUDAO();
            return _dao.UpdateHT_OU(inHT_OU);
        }
        public ReturnValue<bool> InsertHT_OU(HT_OU inHT_OU)
        {
            HT_OUDAO _dao = new HT_OUDAO();
            return _dao.InsertHT_OU(inHT_OU);
        }

        public ReturnValue<HT_OU> InsertHT_OUAndReturn(HT_OU inHT_OU)
        {
            HT_OUDAO _dao = new HT_OUDAO();
            return _dao.InsertHT_OUAndReturn(inHT_OU);
        }

        public ReturnValue<bool> DelHT_OU(Guid id)
        {
            HT_OUDAO _dao = new HT_OUDAO();
            return _dao.DelHT_OU(id);
        }


        public ReturnValue<bool> SynNewData(List<OUDTO> listdata)
        {
            bool result = true;
            if (listdata == null || listdata.Count < 1)
            {
                return new ReturnValue<bool>(false, "Lấy dữ liệu không thành công!");
            }
            HT_OUDAO _dao = new HT_OUDAO();
            _dao.DelAllHT_OU();
            foreach (OUDTO ou in listdata)
            {
                HT_OU inOU = new HT_OU();
                inOU.ID = Guid.NewGuid();
                inOU.Name = ou.Name;
                inOU.DistinguishedName = ou.DistinguishedName;
                result = result && _dao.InsertHT_OU(inOU).RetValue;
            }
            if (result)
            {
                return new ReturnValue<bool>(result, "Đồng bộ thành công " + listdata.Count + " bản ghi!");
            }
            else
            {
                return new ReturnValue<bool>(false, "Lấy dữ liệu không thành công!");
            }
        }

        public string BuildAddOUString(string ouName, string ouPath)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(ouName); sb.Append(Constants.DATA_CUT);
            sb.Append(ouPath);
            return sb.ToString();
        }
        public HT_OU GetAddHT_OUFromData(string data)
        {
            HT_OU RetunOU = new HT_OU();
            string[] Arr = data.Split(Constants.DATA_CUT);
            RetunOU.Name = Arr[0];
            RetunOU.DistinguishedName = Arr[1];
            return RetunOU;
        }

        public bool IsOUExist(string path)
        {
            HT_OUDAO DAO = new HT_OUDAO();
            return DAO.IsOUExist(path);
        }

        public string BuildRenameOUString(Guid ouId, string newOUName)
        {
            StringBuilder sb = new StringBuilder();
            // 1. Append new OU name
            sb.Append(newOUName); sb.Append(Constants.DATA_CUT);
            // 2. Append Old
            HT_OU OldOU = GetHT_OUById(ouId);
            sb.Append(OldOU.ID); sb.Append(Constants.DATA_CUT);
            sb.Append(OldOU.Name); sb.Append(Constants.DATA_CUT);
            sb.Append(OldOU.DistinguishedName); sb.Append(Constants.DATA_CUT);
            return sb.ToString();
        }

        public string GetNewNameFromRenameString(string data)
        {
            HT_OU NewOU = new HT_OU();
            string[] Arr = data.Split(Constants.DATA_CUT);
            return Arr[0];
        }

        public HT_OU GetOldHT_OUFromRenameString(string data)
        {
            HT_OU OldOU = new HT_OU();
            string[] Arr = data.Split(Constants.DATA_CUT);
            OldOU.ID = Guid.Parse(Arr[1]);
            OldOU.Name = Arr[2];
            OldOU.DistinguishedName = Arr[3];
            return OldOU;
        }

        public HT_OU GetHT_OUByDistigutedName(string distigutedName)
        {
            HT_OUDAO _dao = new HT_OUDAO();
            return _dao.GetHT_OUByDistigutedName(distigutedName);
        }

        public string BuildMoveOUString(Guid groupID, string groupName, string groupPath, string newParentPath)
        {
            StringBuilder sb = new StringBuilder();
            // 1. Append new parent path
            sb.Append(newParentPath); sb.Append(Constants.DATA_CUT);
            // 2. Append Old
            sb.Append(groupID); sb.Append(Constants.DATA_CUT);
            sb.Append(groupName); sb.Append(Constants.DATA_CUT);
            sb.Append(groupPath); sb.Append(Constants.DATA_CUT);
            return sb.ToString();
        }


        public string GetNewParentPathFromMoveString(string data)
        {
            HT_OU NewOU = new HT_OU();
            string[] Arr = data.Split(Constants.DATA_CUT);
            return Arr[0];
        }
        public HT_OU GetOldHT_OUFromMoveString(string data)
        {
            HT_OU OldOU = new HT_OU();
            string[] Arr = data.Split(Constants.DATA_CUT);
            OldOU.ID = Guid.Parse(Arr[1]);
            OldOU.Name = Arr[2];
            OldOU.DistinguishedName = Arr[3];
            return OldOU;
        }

        public bool IsOUEmpty(string path)
        {
            // 1. Check if any OU inside
            HT_OUDAO OUDAO = new HT_OUDAO();
            bool hasOU = OUDAO.IsInOU(path);
            if (hasOU) return false;
            
            // 2. Check if any Group inside
            HT_GroupsDAO GroupsDAO = new HT_GroupsDAO();
            bool hasGroups = GroupsDAO.IsInOU(path);
            if (hasGroups) return false;

            // 3. Check if any User inside
            HT_UserDAO UserDAO = new HT_UserDAO();
            bool hasUser = UserDAO.IsInOU(path);
            if (hasUser) return false;
            return true;
        }
    }
}
